const common = {
	toLoginPage() {
		this.reLaunch('/pages/login/login')
	},
	navigateTo(url, params = {}) {
		uni.navigateTo({
			url: url,
			...params,
		})
	},
	msg(title, icon = 'none', mask = false, duration = 1500) {
		//统一提示方便全局修改
		if (Boolean(title) === false) {
			return;
		}
		uni.showToast({
			title,
			duration,
			mask,
			icon: icon
		});
	},
	reLaunch(url) {
		uni.reLaunch({
			url: url
		});
	},
	switchTab(url) {
		uni.switchTab({
			url: url,
		})
	},
	preloadPage(url) {
		uni.preloadPage({
			url: url,
		})
	},
	navigateBack(delta = 1) {
		uni.navigateBack({
			delta: delta,
		})
	},
	detail: (detailInfo) => {
		uni.navigateTo({
			url: '/pages/detail/index?detailInfo=' + encodeURIComponent(JSON.stringify(detailInfo))
		})
	},
	isNullOrEmpty: function(value) {
		//是否为空
		return (value === null || value === '' || value === undefined) ? true : false;
	},
	getLastDayOfMonth: function(date) {
		const currentMonth = date.getMonth()
		const nextMonth = currentMonth + 1
		const nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
		const oneDay = 24 * 60 * 60 * 1000
		return this.timeFormat(new Date(nextMonthFirstDay - oneDay))
	},
	//日期格式化，返回值形式为yy-mm-dd
	timeFormat: function(date) {
		if (!date || typeof(date) === 'string') {
			this.error('参数异常，请检查...')
		}
		let y = date.getFullYear() //年
		let m = date.getMonth() + 1 //月
		let d = date.getDate() //日
		if(m < 10){
		  m = '0' + m;
		}
		return y + '-' + m + '-' + d
	},
	trim(value) {
		//去空格
		return value.replace(/(^\s*)|(\s*$)/g, "");
	},
	isMobile(value) {
		//是否为手机号
		return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value);
	},
	isFloat(value) {
		//金额，只允许保留两位小数
		return /^([0-9]*[.]?[0-9])[0-9]{0,1}$/.test(value);
	},
	isNum(value) {
		//是否全为数字
		return /^[0-9]+$/.test(value);
	},
	checkPwd(value) {
		//密码为8~20位数字和字母组合
		return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(value);
	},
	formatNum(num) {
		//格式化手机号码
		if (utils.isMobile(num)) {
			num = num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
		}
		return num;
	},
	rmoney(money, datil = 2, multiple = 100) {
		//金额格式化
		return parseFloat(Number(money) / multiple).toFixed(datil).toString().split('').reverse().join('').replace(/(\d{3})/g,
			'$1,').replace(
			/\,$/, '').split('').reverse().join('');
	},
	formatValue(value, dotCount) {
		if (value) {
			let val = Number(value);
			if (isNaN(val)) {
				val == 0
			}
			if (dotCount) {
				val.toFixed(Number(dotCount).toFixed(0));
			}
			return val;
		} else {
			return " ";
		}
	},
	formatDate(date, fmt, m, d) {
		if (typeof date == 'string') {
			return date;
		}

		if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";

		if (!date || date == null) return null;
		var o = {
			'M+': date.getMonth() + 1 + Number(m || "0"), // 月份
			'd+': date.getDate() + Number(d || "0"), // 日
			'h+': date.getHours(), // 小时
			'm+': date.getMinutes(), // 分
			's+': date.getSeconds(), // 秒
			'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
			'S': date.getMilliseconds() // 毫秒
		}
		if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1
			.length))
		for (var k in o) {
			if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[
				k]) : (('00' + o[k]).substr(('' + o[k]).length)))
		}
		return fmt
	},
	switchValue(valueStr, needMoney, needFixed, fixedVal) {
		let value = Number(valueStr);
		const val = {
			value: value,
			unit: ''
		};
		if (value >= 10000 && value < 100000000) {
			val.value = (value / 10000);
			val.unit = '万'
			fixedVal = 2
			needFixed = true;
		} else if (value >= 100000000) {
			val.value = (value / 100000000);
			val.unit = '亿'
			fixedVal = 2
			needFixed = true;
		}
		if (needFixed) {
			val.value = val.value.toFixed(fixedVal)
		}
		if (needMoney) {
			val.value = this.rmoney(val.value)
		}
		return val
	},
	isNotBlank(word) {
		return (
			typeof word != "undefined" &&
			word != "undefined" &&
			word != "null" &&
			word != null &&
			word != ""
		);
	},
	isBlank(word) {
		return (
			typeof word == "undefined" ||
			word == "undefined" ||
			word == "null" ||
			word == null ||
			word == ""
		);
	},
	base64ToFile(base64, fileName) {
		let arr = base64.split(",");
		let mime = arr[0].match(/:(.*?);/)[1];
		let bstr = atob(arr[1]);
		let n = bstr.length;
		let u8arr = new Uint8Array(n);
		while (n--) {
			u8arr[n] = bstr.charCodeAt(n);
		}
		return new File([u8arr], fileName, { type: mime });
	},
	//加载配置文件
	loadConfigFile(env) {
		const this_ = this
		//目前小程序没有做验证
		return new Promise(function(resolve, reject) {
			if (env.VUE_APP_PLATFORM == 'app-plus') {
				uni.$tn.vuex("vuex_is_app", true)
				plus.io.resolveLocalFileSystemURL(
					'_www/static/project.config.json',
					(FileEntry) => {
						FileEntry.file((file) => {
							var fileReader = new plus.io.FileReader()
							fileReader.readAsText(file)
							fileReader.onload = function(evt) {
								const option = JSON.parse(evt.target.result)
								let biConfig = {
									...option,
									FULL_API_URL: option.SERVER_URL + option.API_PREFIX_URL
								};
								uni.$tn.vuex("vuex_bi_config", biConfig);
								uni.$biconfig = biConfig;
								resolve();
							}
						})
					}
				)
			} else if (env.VUE_APP_PLATFORM == 'h5') {
				uni.request({
					url: './static/project.config.json', //仅为示例，并非真实接口地址。
					dataType: 'json',
					method: 'GET',
					header: {
						'content-type': 'application/json',
					},
					success: (res) => {
						let biConfig = {
							...res.data,
							FULL_API_URL: res.data.SERVER_URL + res.data.API_PREFIX_URL
						};
						uni.$tn.vuex("vuex_bi_config", biConfig)
						uni.$biconfig = biConfig
						resolve()
					},
					fail: (fail) => {
						reject()
					},
				})
			}
		})
	},

}

uni.$common = common

module.exports = {
	...common,
}